package com.simple.elpmobile.onlinecase.ui.component;


import java.util.ArrayList;
import java.util.List;

import com.simple.elpmobile.domain.CaseInfo;
import com.simple.elpmobile.domain.Doctor;
import com.simple.elpmobile.onlinecase.R;
import com.simple.elpmobile.onlinecase.common.data.BaseGlobalVaribles;
import com.simple.elpmobile.onlinecase.engine.AppEngine;
import com.simple.elpmobile.onlinecase.engine.IManager;
import com.simple.elpmobile.onlinecase.engine.manager.NetworkManager;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommanHandler;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommitHandler;
import com.simple.elpmobile.onlinecase.ui.base.CustomToast;
import com.simple.elpmobile.onlinecase.ui.base.LoadingDialog;
import android.app.Activity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class FragmetTab2 extends Fragment implements OnClickListener {
	
	private EditText solutionEdit;
	private Spinner docSpinner;
	private Button commitBtn;
	private LoadingDialog loadingDialog;
	private int id=0;
	
	public static FragmetTab2 newInstance() {
		FragmetTab2 newFragment = new FragmetTab2();
		return newFragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view= inflater.inflate(R.layout.tab2_layout, container, false);
		initUI(view);
		return view;
	}

	
	public void initUI(View view){
		solutionEdit=(EditText)view.findViewById(R.id.solution_eidt);
		docSpinner=(Spinner)view.findViewById(R.id.doc_sp);
		commitBtn=(Button)view.findViewById(R.id.commit_btn);
		commitBtn.setOnClickListener(this); 
		docSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				id=BaseGlobalVaribles.doctors.get(arg2).getId();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	
	
	
	
	@Override
	public void onResume() {
		super.onResume();
		initData();
	}

	@Override
	public View getView() {
		return super.getView();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		
		case R.id.commit_btn:
			commitCase();
			break;

		default:
			break;
		}
		
	}
	
	public void commitCase(){
		String info=solutionEdit.getText().toString().trim();
		if (TextUtils.isEmpty(info)) {
			CustomToast.showToast(getActivity(), "您没有填写病例不能提交",1000 );
			return;
		}
		CaseInfo caseInfo=new CaseInfo();
		caseInfo.setPatientId(BaseGlobalVaribles.patientInfo.getId());
		caseInfo.setDoctorId(id);
		caseInfo.setPatientInfo(info);
		
		showLoading("正在提交数据...");
		((NetworkManager) (AppEngine.getInstance()
				.getManager(IManager.NETWOTK_ID))).commitCaseInfo(caseInfo, new CommitHandler() {
					
					@Override
					public void OnSucess(String info) {
						dismissLoading();
						CustomToast.showToast(getActivity(),"您的病例提交成功！", 1000);
						solutionEdit.setText("");
						
					}
					
					@Override
					public void OnFail(int code, String str) {
						dismissLoading();
						CustomToast.showToast(getActivity(),str, 1000);
					}
				});
	}
	
	public void initData(){
		showLoading("数据加载中...");
		((NetworkManager) (AppEngine.getInstance()
				.getManager(IManager.NETWOTK_ID))).getDoctorInfo( new CommanHandler() {
					@Override
					public void onLoginSuccess(Object obj) {
						dismissLoading();
						List<Doctor> doctors=(ArrayList<Doctor>)obj;
						BaseGlobalVaribles.doctors=doctors;
						if (BaseGlobalVaribles.doctors!=null) {
							docSpinner.setAdapter(new MyAdapter());
						}
					}

					@Override
					public void onLoginFailed(String errorDescription) {
						dismissLoading();
						CustomToast.showToast(getActivity(),errorDescription, 1000);
					}
				});
	}
	
	
	
	private final String DIALOG_LOCKER = "dialogLocker";

	private void showLoading(String msg) {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
			loadingDialog = new LoadingDialog(getActivity());
			loadingDialog.showDialog(msg);
		}
	}

	private void dismissLoading() {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
		}
	}
	
	
	class MyAdapter extends BaseAdapter{
		List<Doctor> list =BaseGlobalVaribles.doctors;
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public int getItemViewType(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View view=LayoutInflater.from(getActivity()).inflate(R.layout.sex_item, null);
			TextView docTxt=(TextView)view.findViewById(R.id.sexitem);
			docTxt.setText(list.get(arg0).getName());
			return view;
		}
		
	} 
	
}
